package com.vctgo.accountSet.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vctgo.accountSet.domain.Chinas;
import com.vctgo.accountSet.service.IChinasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.accountSet.domain.TbSetAccounts;
import com.vctgo.accountSet.service.ITbSetAccountsService;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 账套Controller
 *
 * @author lhc
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/accounts")
public class TbSetAccountsController extends BaseController
{
    @Autowired
    private ITbSetAccountsService tbSetAccountsService;
    @Autowired
    private IChinasService chinasService;
    /**
     * 查询账套列表
     */
    @RequiresPermissions("accountSet:accounts:list")
    @GetMapping("/list")
    public AjaxResult list(TbSetAccounts tbSetAccounts)
    {
        IPage<TbSetAccounts> list = tbSetAccountsService.selectTbSetAccountsPage(tbSetAccounts);
        return AjaxResult.success(list);
    }

    /**
     * 导出账套列表
     */
    @RequiresPermissions("accountSet:accounts:export")
    @Log(title = "账套", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSetAccounts tbSetAccounts)
    {
        List<TbSetAccounts> list = tbSetAccountsService.selectTbSetAccountsList(tbSetAccounts);
        ExcelUtil<TbSetAccounts> util = new ExcelUtil<TbSetAccounts>(TbSetAccounts.class);
        util.exportExcel(response, list, "账套数据");
    }

    /**
     * 获取账套详细信息
     */
    @RequiresPermissions("accountSet:accounts:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        List<String> strings = new ArrayList<>();

        TbSetAccounts tbSetAccounts = tbSetAccountsService.selectTbSetAccountsById(id);
        String accountAddressId = tbSetAccounts.getAccountAddressId();

        String[] split = accountAddressId.split(",");
        for (String s : split) {
            strings.add(s);
        }

//        List<Chinas> chinas = chinasService.selectLinkage(strings);
        QueryWrapper<Chinas> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Chinas::getCode,strings);

        List<Chinas> chinasList = chinasService.selectnihao(wrapper);

        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("accounts",tbSetAccounts);
        stringObjectMap.put("chinas",chinasList);

        return success(stringObjectMap);
    }

    /**
     * 新增账套
     */
    @RequiresPermissions("accountSet:accounts:add")
    @Log(title = "账套", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSetAccounts tbSetAccounts)
    {
        return toAjax(tbSetAccountsService.insertTbSetAccounts(tbSetAccounts));
    }

    /**
     * 修改账套
     */
    @RequiresPermissions("accountSet:accounts:edit")
    @Log(title = "账套", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSetAccounts tbSetAccounts)
    {
        return toAjax(tbSetAccountsService.updateTbSetAccounts(tbSetAccounts));
    }

    /**
     * 删除账套
     */
    @RequiresPermissions("accountSet:accounts:remove")
    @Log(title = "账套", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbSetAccountsService.deleteTbSetAccountsByIds(ids));
    }
}
